package myservice.android.activities;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;

import myservice.android.Global;
import myservice.android.R;
import myservice.android.database.Database;
import myservice.android.utilities.AddressComparator;
import myservice.android.utilities.CheckBoxMonitor;
import myservice.android.utilities.EditTextMonitor;
import myservice.android.utilities.PopupMenu;
import myservice.android.utilities.Record;
import myservice.android.utilities.RecordAdapter;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class TerritoryElementActivity extends BaseSaveableActivity {
	
	private Button addElementButton, deleteButton, addCallButton, saveButton;
	private ArrayList<Record> listData;
	private ListView list;
	private TextView noElementsDefinedText;
	private ImageView elementIcon;
	private RecordAdapter listAdapter;
	private EditText nameText,commentsText;
	private CheckBox completedCheckBox;

	private long territoryId;
	private int territoryType; // 0=territory (can hold 1,2,3), 1=street (can hold 2,3), 2=single-family house, 3=apartment house (can hold 4,5), 4=staircase (can hold 5), 5=apartment 	
	private int mode; // 0 = modify elements; 1 = pick an element
	
	@Override
	protected void initializeLayoutIdAndTitle() {
		layoutId = R.layout.territory_element;
	}

	@Override
	protected void retrieveViews() {
		
    	addCallButton = (Button) findViewById(R.id.addCallButton);
    	addElementButton = (Button) findViewById(R.id.addElementButton);
    	saveButton = (Button) findViewById(R.id.saveButton); 
    	deleteButton = (Button) findViewById(R.id.deleteButton);    	
        elementIcon = (ImageView) findViewById(R.id.elementIcon);
        list = (ListView) findViewById(R.id.buildingList);
        noElementsDefinedText = (TextView) findViewById(R.id.noElementsMessage);
        nameText = (EditText) findViewById(R.id.nameText);
        commentsText = (EditText) findViewById(R.id.commentsText); 
        completedCheckBox = (CheckBox) findViewById(R.id.completedCheckbox);
		
	}

	@Override
	protected void loadActivityParameters() {
		
		territoryId = activityParameters.getLong("territory_id");
	    mode = activityParameters.getInt("mode");
		
	}

	@Override
	protected void assignListeners() {
		
		commentsText.addTextChangedListener(new EditTextMonitor(this));
		nameText.addTextChangedListener(new EditTextMonitor(this));
		completedCheckBox.setOnCheckedChangeListener(new CheckBoxMonitor(this));		
        
        saveButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				save();
				finish();
			}
        });        
        
        deleteButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				
				String message;
				if (territoryType==0)
					message=Global.res.getString(R.string.message_delete_territory_confirmation);
				else if (territoryType==1)
					message=Global.res.getString(R.string.message_delete_street_confirmation);
				else if (territoryType==2)
					message=Global.res.getString(R.string.message_delete_house_confirmation);
				else if (territoryType==3)
					message=Global.res.getString(R.string.message_delete_apartment_house_confirmation);
				else if (territoryType==4)
					message=Global.res.getString(R.string.message_delete_staircase_confirmation);
				else
					message=Global.res.getString(R.string.message_delete_apartment_confirmation);				
				
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(TerritoryElementActivity.this);
				alertDialogBuilder.setTitle(Global.res.getText(R.string.title_warning));				
				alertDialogBuilder
					.setMessage(message)
					.setPositiveButton(Global.res.getText(R.string.button_yes),new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,int id) {
							Database.deleteTerritoryElement(territoryId);
							finish();
						}
					  })
					.setNegativeButton(Global.res.getText(R.string.button_no),new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});
		 
				AlertDialog alertDialog = alertDialogBuilder.create();
				alertDialog.show();				
			}        	
        });        
        
        addElementButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {				
				
				final Dialog dialog = new Dialog(TerritoryElementActivity.this);
				dialog.setContentView(R.layout.add_territory_element_dialog);
				dialog.setTitle(Global.res.getString(R.string.title_add_new_territory_element));
				
				Button saveButton = (Button) dialog.findViewById(R.id.saveButton);				
				Button cancelButton = (Button) dialog.findViewById(R.id.cancelButton);
				final EditText nameField = (EditText) dialog.findViewById(R.id.nameField);
				final Spinner type = (Spinner) dialog.findViewById(R.id.elementTypeSpinner);
				String[] elements = new String[0];				
				if (territoryType==0) { // territory
					elements = new String[3];
			        elements[0]=Global.res.getString(R.string.label_element_house);
			        elements[1]=Global.res.getString(R.string.label_element_apartment_house);
			        elements[2]=Global.res.getString(R.string.label_element_street);			        
				} else if (territoryType==1) { // street
					elements = new String[2];
			        elements[0]=Global.res.getString(R.string.label_element_house);
			        elements[1]=Global.res.getString(R.string.label_element_apartment_house);
			        nameField.setRawInputType(Configuration.KEYBOARD_QWERTY);
				} else if (territoryType==3) { // apartment house
					elements = new String[2];
			        elements[0]=Global.res.getString(R.string.label_element_apartment);					
			        elements[1]=Global.res.getString(R.string.label_element_staircase);
			        nameField.setRawInputType(Configuration.KEYBOARD_QWERTY);
				} else if (territoryType==4) { // staircase
					elements = new String[1];
			        elements[0]=Global.res.getString(R.string.label_element_apartment);	
			        nameField.setRawInputType(Configuration.KEYBOARD_QWERTY);
				}
				
		        ArrayAdapter<String> adapter = new ArrayAdapter<String>(TerritoryElementActivity.this,android.R.layout.simple_spinner_item, elements);
		        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);			        
		        type.setAdapter(adapter);				
				
				saveButton.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						int t=0;
						if (territoryType==0) { // territory
							switch (type.getSelectedItemPosition()) {
								case 0: t=2; break;
								case 1: t=3; break; 
								case 2: t=1; break;
							}
						} else if (territoryType==1) { // street
							t=type.getSelectedItemPosition()+2;
						} else if (territoryType==3) { // apartment house
							if (type.getSelectedItemPosition()==0)
								t=5; // apartment
							else
								t=4; // staircase 
						} else if (territoryType==4) { // staircase
							t=5;
						}										    	
						
						Database.execSQL("INSERT INTO territories (territoryid,type,parent,name,comments,completed,timestamp) VALUES ("+Database.getUniqueRowId("territories")+
								         ","+t+","+territoryId+",'"+Global.fixText(nameField.getText())+"','',0,"+Global.getTimeStamp()+")");
						dialog.dismiss();
						updateList();
					}			
				});
				
				cancelButton.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						dialog.dismiss();				
					}			
				});		
				
				dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);				
				dialog.show();									
			}
        });              
        
        addCallButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				
				final Dialog dialog = new Dialog(TerritoryElementActivity.this);
				dialog.setContentView(R.layout.add_call_dialog);
				dialog.setTitle(Global.res.getString(R.string.title_add_new_call));
				
				Button saveButton = (Button) dialog.findViewById(R.id.saveButton);				
				Button cancelButton = (Button) dialog.findViewById(R.id.cancelButton);
				final EditText commentsField = (EditText) dialog.findViewById(R.id.commentsField);
				
				saveButton.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {					
						Calendar today = Calendar.getInstance();							
						Database.execSQL("INSERT INTO calls (callid,date,month,year,personid,territoryid,comments,biblestudy,timestamp) VALUES ("+Database.getUniqueRowId("calls")+
								         ","+today.get(Calendar.DATE)+","+today.get(Calendar.MONTH)+","+today.get(Calendar.YEAR)+",0,"+territoryId+
								         ",'"+Global.fixText(commentsField.getText())+"',0,"+Global.getTimeStamp()+")");	
						dialog.dismiss();
						updateList();
					}			
				});
				
				cancelButton.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						dialog.dismiss();				
					}			
				});		
				
				dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);								
				dialog.show();							
			}
        }); 
		
	} 		       
    
    @Override
    protected void save() {
    	
		activityContentModified = false;
    	
    	commentsText.clearFocus();
    	nameText.clearFocus();
    	
    	int completed = 0;
    	if (completedCheckBox.isChecked())
    		completed = 1;
    	
    	Database.execSQL("UPDATE territories SET name='"+Global.fixText(nameText.getText())+"', comments='"+Global.fixText(commentsText.getText())+"', timestamp="+
                         Global.getTimeStamp()+", completed="+completed+" WHERE territoryid="+territoryId);    	
    }
    
    private void updateList() {
    	
		PopupMenu.createPopupMenu(this);		
		PopupMenu.menuSection4.setVisibility(View.GONE);
		
		listData = new ArrayList<Record>();		
		
		if (territoryType!=2 && territoryType!=5) { // if not apartment or house, select child territory elements 				
			
	    	boolean noElementsDefined = true;
			
			// Sort the elements numerically
	    	
			// Start with incompleted elements
			Cursor cursor = Database.select("SELECT * FROM territories WHERE parent="+territoryId+" AND timestamp<>"+Global.TIMESTAMP_DELETED+" AND completed=0");			

	    	while (cursor.moveToNext()) {
	    		noElementsDefined=false;
	    		Record rec = new Record();
	    		rec.id=cursor.getInt(cursor.getColumnIndex("territoryid"));
	    		rec.status=cursor.getInt(cursor.getColumnIndex("type"));
	    		rec.firstLine=cursor.getString(cursor.getColumnIndex("name"));
	    		rec.secondLine=cursor.getString(cursor.getColumnIndex("comments"));
	    		rec.completed=0;	 
	    		
	    		// Mark visited houses and apartments
	    		if (rec.status==2 || rec.status==5) {
	    			Cursor c = Database.select("SELECT COUNT(*) AS c FROM calls WHERE territoryid="+rec.id+" AND timestamp<>"+Global.TIMESTAMP_DELETED);
	    			c.moveToNext();
	    			if (c.getInt(c.getColumnIndex("c"))>0) {
	    				if (rec.status==2)
	    					rec.status=7; // visited house
	    				else
	    					rec.status=6; // visited apartment
	    			}
	    			c.close();
	    		}	    		
	    		
	    		listData.add(rec);
	    	}	
	    	cursor.close(); 	    	
			
			// Now obtain completed elements
	    	ArrayList<Record> completedElements = new ArrayList<Record>();	
			cursor = Database.select("SELECT * FROM territories WHERE parent="+territoryId+" AND timestamp<>"+Global.TIMESTAMP_DELETED+" AND completed=1");			

	    	while (cursor.moveToNext()) {
	    		noElementsDefined=false;
	    		Record rec = new Record();
	    		rec.id=cursor.getInt(cursor.getColumnIndex("territoryid"));
	    		rec.status=cursor.getInt(cursor.getColumnIndex("type"));
	    		rec.firstLine=cursor.getString(cursor.getColumnIndex("name"));
	    		rec.secondLine=cursor.getString(cursor.getColumnIndex("comments"));
	    		rec.completed=cursor.getInt(cursor.getColumnIndex("completed"));
	    		
	    		// Mark visited houses and apartments
	    		if (rec.status==2 || rec.status==5) {
	    			Cursor c = Database.select("SELECT COUNT(*) AS c FROM calls WHERE territoryid="+rec.id+" AND timestamp<>"+Global.TIMESTAMP_DELETED);
	    			c.moveToNext();
	    			if (c.getInt(c.getColumnIndex("c"))>0) {
	    				if (rec.status==2)
	    					rec.status=7; // visited house
	    				else
	    					rec.status=6; // visited apartment
	    			}
	    			c.close();
	    		}
	    		
	    		completedElements.add(rec);
	    	}	
	    	cursor.close();  
	    	
	    	// Sort both lists numerically	    	
	    	Collections.sort(listData, new AddressComparator());
	    	Collections.sort(completedElements, new AddressComparator());
	    	
	    	// Append completed elements to the end of listData
	    	for (int i=0; i<completedElements.size(); i++)
	    		listData.add(completedElements.get(i));
	    	
	    	if (noElementsDefined==true)
	    		noElementsDefinedText.setVisibility(View.VISIBLE);
	    	else
	    		noElementsDefinedText.setVisibility(View.GONE);
	    	
	        listAdapter = new RecordAdapter(this, R.layout.list_row, listData, 3);
	        list.setAdapter(listAdapter);
	        
	        list.setOnItemLongClickListener(new OnItemLongClickListener () {

				@Override
				public boolean onItemLongClick(AdapterView<?> parent, View view,
						int position, long id) {
					
					final long tid = listData.get(position).id;
					final int clickedType = listData.get(position).status;
					final int completed = listData.get(position).completed;
					
					PopupMenu.menuHeading.setText(listData.get(position).firstLine);
					
					PopupMenu.menuOption1.setText(Global.res.getString(R.string.menuitem_territory_modify));	
					if (completed==0)
						PopupMenu.menuOption2.setText(Global.res.getString(R.string.menuitem_territory_mark_completed));
					else
						PopupMenu.menuOption2.setText(Global.res.getString(R.string.menuitem_territory_mark_not_completed));
					PopupMenu.menuOption3.setText(Global.res.getString(R.string.menuitem_territory_delete));				
					
					PopupMenu.menuOption1.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							PopupMenu.closeMenu();
							
							save();
							
							Intent i = new Intent(getApplicationContext(),TerritoryElementActivity.class);
							i.putExtra("mode",0);
							i.putExtra("territory_id",tid);
							startActivity(i);				
						}					
					});	
					
					PopupMenu.menuOption2.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							PopupMenu.closeMenu();							
							
							int c = (completed + 1) % 2;							
							
							Database.execSQL("UPDATE territories SET completed="+c+", timestamp="+Global.getTimeStamp()+" WHERE territoryid="+tid);
							
							updateList();
						}					
					});						
					
					PopupMenu.menuOption3.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							PopupMenu.closeMenu();
							
							String message = "";
							
							if (clickedType==1)
								message = Global.res.getString(R.string.message_delete_street_confirmation);
							else if (clickedType==2)
								message = Global.res.getString(R.string.message_delete_house_confirmation);
							else if (clickedType==3)
								message = Global.res.getString(R.string.message_delete_apartment_house_confirmation);
							else if (clickedType==4)
								message = Global.res.getString(R.string.message_delete_staircase_confirmation);
							else if (clickedType==5)
								message = Global.res.getString(R.string.message_delete_apartment_confirmation);							
							
							AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(TerritoryElementActivity.this);
							alertDialogBuilder.setTitle(Global.res.getText(R.string.title_warning));				
							alertDialogBuilder
								.setMessage(message)
								.setPositiveButton(Global.res.getText(R.string.button_yes),new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,int id) {
										Database.deleteTerritoryElement(tid);							    	
										updateList();
									}
								  })
								.setNegativeButton(Global.res.getText(R.string.button_no),new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog, int id) {
										dialog.cancel();
									}
								});
					 
							AlertDialog alertDialog = alertDialogBuilder.create();
							alertDialog.show();		
						}					
					});					
					
					PopupMenu.showMenu(view);				
					
					return true;
				}
	        	
	        });	        
	        
	        list.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position,
						long id) {
					
					save();
					
					long tid = listData.get(position).id;
					int clickedType = listData.get(position).status;
					
					if ((clickedType==2 || clickedType==5) && mode==1) { // user is selecting a house or apartment from a list
						Global.selectedId=tid;
						Global.selectedType=1;
						finish();						
					} else {						
						Intent i = new Intent(getApplicationContext(),TerritoryElementActivity.class);
						i.putExtra("mode",mode);
						i.putExtra("territory_id",tid);
						startActivity(i);
					}

				}
	        	
	        });			
			
		}
		else { // select calls
			
			PopupMenu.menuSection3.setVisibility(View.GONE);
			
			Cursor cursor = Database.select("SELECT date,month,year,callid,biblestudy,comments FROM calls WHERE territoryid="+territoryId+
					                 " AND timestamp<>"+Global.TIMESTAMP_DELETED+" ORDER BY year,month,date");
			
	    	boolean noCallsDefined = true;

	    	while (cursor.moveToNext()) {
	    		noCallsDefined=false;
	    		Record rec = new Record();
	    		int date = cursor.getInt(cursor.getColumnIndex("date"));
	    		int month = cursor.getInt(cursor.getColumnIndex("month"));
	    		int year = cursor.getInt(cursor.getColumnIndex("year"));
	    		rec.id=cursor.getInt(cursor.getColumnIndex("callid"));
	    		rec.status=cursor.getInt(cursor.getColumnIndex("biblestudy"));
	    		rec.firstLine=Global.getFormattedDate(year, month, date);
	    		rec.secondLine=cursor.getString(cursor.getColumnIndex("comments"));
	    		listData.add(rec);
	    	}	
	    	cursor.close();    	    	
	    	
	    	if (noCallsDefined==true)
	    		noElementsDefinedText.setVisibility(View.VISIBLE);
	    	else
	    		noElementsDefinedText.setVisibility(View.GONE);
	    	
	        listAdapter = new RecordAdapter(this, R.layout.list_row, listData, 1);
	        list.setAdapter(listAdapter);    
	        
	        list.setOnItemLongClickListener(new OnItemLongClickListener () {

				@Override
				public boolean onItemLongClick(AdapterView<?> parent, View view,
						int position, long id) {
					
					final long tid = listData.get(position).id;
					
					PopupMenu.menuHeading.setText(listData.get(position).firstLine);
					
					PopupMenu.menuOption1.setText(Global.res.getString(R.string.menuitem_territory_modify));				
					PopupMenu.menuOption2.setText(Global.res.getString(R.string.menuitem_territory_delete));					
					
					PopupMenu.menuOption1.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							PopupMenu.closeMenu();
							
							save();
							
							Intent i = new Intent(getApplicationContext(),CallActivity.class);
							i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
							i.putExtra("call_id",tid);
							startActivity(i);				
						}					
					});					
					
					PopupMenu.menuOption2.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View arg0) {
							PopupMenu.closeMenu();													
							
							AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(TerritoryElementActivity.this);
							alertDialogBuilder.setTitle(Global.res.getText(R.string.title_warning));				
							alertDialogBuilder
								.setMessage(Global.res.getString(R.string.message_delete_call_confirmation))
								.setPositiveButton(Global.res.getText(R.string.button_yes),new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,int id) {
										Database.deleteCall(tid);							    	
										updateList();
									}
								  })
								.setNegativeButton(Global.res.getText(R.string.button_no),new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog, int id) {
										dialog.cancel();
									}
								});
					 
							AlertDialog alertDialog = alertDialogBuilder.create();
							alertDialog.show();		
						}					
					});					
					
					PopupMenu.showMenu(view);				
					
					return true;
				}
	        	
	        });		        
	        
	        list.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position,
						long id) {					
					save();															
					Intent i = new Intent(getApplicationContext(),CallActivity.class);
					i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
					i.putExtra("call_id",listData.get(position).id);
					startActivity(i);					
				}
	        	
	        });				
		}
    	
    }
    
    @Override
	protected void updateUi() {
		
		if (Global.selectedId!=0) // user has picked a house or apartment from list
			finish();			
		
		Cursor cursor;		
		cursor = Database.select("SELECT type,name,comments,completed FROM territories WHERE territoryid="+territoryId);
		cursor.moveToNext();
		territoryType=cursor.getInt(cursor.getColumnIndex("type")); 
		nameText.setText(cursor.getString(cursor.getColumnIndex("name")));
		if (nameText.length()<1)
			nameText.requestFocus();
		commentsText.setText(cursor.getString(cursor.getColumnIndex("comments")));
		if (cursor.getInt(cursor.getColumnIndex("completed"))==0)
			completedCheckBox.setChecked(false);
		else
			completedCheckBox.setChecked(true);
		cursor.close();
		
		if (territoryType==0) { // territory
			addCallButton.setVisibility(View.GONE);	
			elementIcon.setImageDrawable(Global.res.getDrawable(R.drawable.territory));
			noElementsDefinedText.setText(Global.res.getString(R.string.message_no_streets_houses_defined));
		} else if (territoryType==1) { // street			
			addCallButton.setVisibility(View.GONE);
			elementIcon.setImageDrawable(Global.res.getDrawable(R.drawable.street));
			noElementsDefinedText.setText(Global.res.getString(R.string.message_no_houses_defined));			
		} else if (territoryType==2) { // house
			addElementButton.setVisibility(View.GONE);
			elementIcon.setImageDrawable(Global.res.getDrawable(R.drawable.house));		
			noElementsDefinedText.setText(Global.res.getString(R.string.message_no_calls_recorded));			
		} else if (territoryType==3) { // apartment house			
			addCallButton.setVisibility(View.GONE);
			elementIcon.setImageDrawable(Global.res.getDrawable(R.drawable.apartment_house));	
			noElementsDefinedText.setText(Global.res.getString(R.string.message_no_staircases_apartments_defined));			
		} else if (territoryType==4) { // staircase
			addCallButton.setVisibility(View.GONE);
			elementIcon.setImageDrawable(Global.res.getDrawable(R.drawable.staircase));	
			noElementsDefinedText.setText(Global.res.getString(R.string.message_no_apartments_defined));			
		} else if (territoryType==5) { // apartment	
			addElementButton.setVisibility(View.GONE);			
			elementIcon.setImageDrawable(Global.res.getDrawable(R.drawable.apartment));
	    	noElementsDefinedText.setText(Global.res.getString(R.string.message_no_calls_recorded));			
		}
		
    	if (mode==0) {
    		if (territoryType==0)
    			activityTitleView.setText(Global.res.getString(R.string.title_territory));
    		else if (territoryType==1)
    			activityTitleView.setText(Global.res.getString(R.string.title_street));
    		else if (territoryType==2)
    			activityTitleView.setText(Global.res.getString(R.string.title_house));
    		else if (territoryType==3)
    			activityTitleView.setText(Global.res.getString(R.string.title_apartment_house));
    		else if (territoryType==4)
    			activityTitleView.setText(Global.res.getString(R.string.title_staircase));    
    		else if (territoryType==5)
    			activityTitleView.setText(Global.res.getString(R.string.title_apartment));      		
    	}
    	else {
    		if (territoryType==3 || territoryType==4)
    			activityTitleView.setText(Global.res.getString(R.string.title_select_apartment));
    		else
    			activityTitleView.setText(Global.res.getString(R.string.title_select_house));    		
    	}	
    	
    	updateList();
		    	    	
	}
    
}